"""
Dict isa dictionary with self-initializing key values.

This code is part of AXE:           /'-./\_
cropping complexity,                :   ||,>
culling confusion,                  \.-'||
and cutting the crap                    ||
since 2012.                             ||  
                                        ||   pjb

(c) 2012, Tim Menzies, tim@menzies.us, LGPL v3.0
"""

from lib import *

class Dict(dict):
  def inc(self,key,n=1):
    "Add 1 to self['key']."
    out = self[key] = self.get(key,0) + n
    return out

  def push(self,key,val):
    "Add 'val' to a list stored in 'key',"
    out = self[key] = self.get(key,[])
    out.append(val)
    return out

  def at(self,key,default) :
    """
    Get a value from self['key'] or initialize
    it from 'default'. Note that 'default' can
    be a lambda body to call, or some other value.
    """
    if self.has_key(key) :
      return self[key]
    else:
      out = self[key] = val(default)
      return out
    
